using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Workflows.Components.FileManipulation.Properties;

namespace Workflows.Components.FileManipulation
{
    /// <summary>
    /// ftp download/upload
    /// </summary>
    [Serializable]
    public class FtpComponent:WorkflowComponentBase
    {
        private FtpInstruction _Instruction;
        private FtpOutput _Output;
        /// <summary></summary>
        public FtpProtocol Protocol
        {
            get { return this._Instruction.Protocol; }
            set { this._Instruction.Protocol = value; }
        }
        /// <summary>either upload or download</summary>
        public FtpDirection Direction
        {
            get { return this._Instruction.Direction; }
            set { this._Instruction.Direction = value; }
        }

        /// <summary></summary>
        public string FtpSite
        {
            get
            {
                return this._Instruction.FtpSite;
            }
            set
            {
                this._Instruction.FtpSite = value;
            }
        }

        /// <summary></summary>
        public string SrcPath
        {
            get
            {
                return this._Instruction.SrcPath;
            }
            set
            {
                this._Instruction.SrcPath = value;
            }
        }

        /// <summary></summary>
        public string TgtFolder
        {
            get
            {
                return this._Instruction.TgtFolder;
            }
            set
            {
                this._Instruction.TgtFolder = value;
            }
        }

        /// <summary></summary>
        public string UserName
        {
            get { return this._Instruction.UserName; }
            set { this._Instruction.UserName = value; }
        }
        /// <summary></summary>
        public string Password
        {
            get { return this._Instruction.Password; }
            set { this._Instruction.Password = value; }
        }

        /// <summary></summary>
        public bool Anonymous
        {
            get
            {
                return this._Instruction.Anonymous;
            }
            set
            {
                this._Instruction.Anonymous = value;
            }
        }

        /// <summary></summary>
        public bool WindowsAuth
        {
            get
            {
                return this._Instruction.WindowsAuth;
            }
            set
            {
                this._Instruction.WindowsAuth = value;
            }
        }

        /// <summary></summary>
        public override InstructionBase Instruction
        {
            get { return this._Instruction; }
            set { this._Instruction=(FtpInstruction)value; }
        }

        public override ResultBase Output
        {
            get { return this._Output; }
            set { this._Output=(FtpOutput)value; }
        }
        /// <summary></summary>
        public FtpComponent():base()
        {
            this.ComponentActivityType = WorkflowActivityType.SimpleActivity;
            this.ComponentCategory = WorkflowComponentCategory.File_Tasks.ToString();
            this.Description = "Download or upload files using ftp protocols";
            this.ComponentIcon = Resources.dataTransfer;
            this.ComponentImage = Resources.fieltransfer;
            this.ComponentName = "FTP task";

            this._Instruction =
                new FtpInstruction(this.ComponentID, Guid.Empty, string.Empty,
                                   FtpProtocol.Ftp, FtpDirection.Download, string.Empty,
                                   string.Empty, string.Empty, string.Empty, false, false);
            this._Output=new FtpOutput(this.ComponentID,Guid.Empty,string.Empty);
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FtpSite", this.FtpSite);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FtpProtocol", ((int) this.Protocol).ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FtpDirection", ((int) this.Direction).ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SrcFilePath", this.SrcPath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TgtFolderPath", this.TgtFolder);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "UserName", this.UserName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Password", this.Password);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Anonymous", this.Anonymous.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WindowsAuth", this.WindowsAuth.ToString());

            return xNode;
        }

        public override WorkflowComponentBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                FtpComponent component = base.Instantiate(xNode) as FtpComponent;

                component.FtpSite = xNode.Attributes.GetNamedItem("FtpSite").Value;
                component.Protocol = (FtpProtocol) int.Parse(xNode.Attributes.GetNamedItem("FtpProtocol").Value);
                component.Direction = (FtpDirection) int.Parse(xNode.Attributes.GetNamedItem("FtpDirection").Value);
                component.SrcPath = xNode.Attributes.GetNamedItem("SrcFilePath").Value;
                component.TgtFolder = xNode.Attributes.GetNamedItem("TgtFolderPath").Value;
                component.UserName = xNode.Attributes.GetNamedItem("UserName").Value;
                component.Password = xNode.Attributes.GetNamedItem("Password").Value;
                component.Anonymous = bool.Parse(xNode.Attributes.GetNamedItem("Anonymous").Value);
                component.WindowsAuth = bool.Parse(xNode.Attributes.GetNamedItem("WindowsAuth").Value);
               
                return component;
            }
            else
            {
                throw new Exception("Invalid type to instantiate");
            }
        }
    }
}
